package com.itboye.sunsun.login.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.itboye.sunsun.MainActivity;
import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.network.LoginNetWork;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class RegistActivity extends BaseActivity implements OnClickListener{
	private EditText phoneedit;
	private EditText yzmedit;
	private EditText passwordedit;
	private EditText inviteedit;
	private String username;
	private String yzm;
	private String password;
	private String invite;
	private Button yzmbutton;
	private Button registbutton;
	private RelativeLayout backrela;
	private TimeCount time;
	protected String code;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		//initData();
		time = new TimeCount(60000, 1000,yzmbutton);// 构造CountDownTimer对象
		
	}

	@Override
	public void onClick(View v) {
                  
		switch(v.getId())
		{
		case R.id.yzmbutton:
			username=phoneedit.getText().toString();
			if(!username.equals(""))
			{
			   getyzm();
			   time.start();
			}else
			{
				Toast.makeText(RegistActivity.this, "请输入手机号", 0).show();
			}
			break;
		case R.id.registbutton:
			userregist();
			break;
		case R.id.backrela:
			this.finish();
			break;
		default:
		break;
		}
	}
	
	private void getyzm()
	{
		MyJsonRequest<String>request= new MyJsonRequest.Builder<String>()
				 .apiVer("100")
				 .typeKey("BY_Message_send_sms")
				 .param("mobile", phoneedit.getText().toString())
				 .param("code_type", "1")
				 .requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
//						// TODO Auto-generated method stub
//						DebugLog.v("result", response.toString());	
//						yzmedit.setText(response);
						code=response;
					}
				})
				 .errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if(exception instanceof CodeErrorException)
						{
							Toast.makeText(App.ctx, msg, 0).show();
						}else
						{
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}
						
					}
				})
				.build();
				HttpRequest.getDefaultRequestQueue().add(request);
	}
	private void userregist()
	{
		if(yzmedit.getText().toString().trim()==null){
			Toast.makeText(this, "请输入验证码", 0).show();
			return ;
		}
		MyJsonRequest<String>request= new MyJsonRequest.Builder<String>()
				 .apiVer("100")
				 .typeKey("BY_User_register")
				 .param("from","0")
				 .param("code_type", "1")
				 .param("reg_type", "3")
				 .param("password", ""+passwordedit.getText().toString())
				 .param("code",yzmedit.getText().toString())
				 .param("invite_id",""+inviteedit.getText().toString())
				 .param("username",  ""+phoneedit.getText().toString())
				 .requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());	
						//Toast.makeText(App.ctx, "注册成功", 0).show();
						LoginController.setLoginState(new LoginedState());
						SPUtils.put(App.ctx, null, SPContants.USER_ID, response);
						SPUtils.put(App.ctx, null, SPContants.USERNAME, phoneedit.getText().toString());
						SPUtils.put(App.ctx, null, SPContants.MOBILE, phoneedit.getText().toString());
					//	SPUtils.put(App.ctx, null, SPContants.PASSWORD, passwordedit.getText().toString());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);
						
						login();
						RegistActivity.this.finish();
						
						//发送广播通知LoginActivity结束
						Intent intent1=new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());
					
					}
				})
				 .errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if(exception instanceof CodeErrorException)
						{
							Toast.makeText(App.ctx, msg, 0).show();
						}else
						{
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}
						
					}
				})
				.build();
				HttpRequest.getDefaultRequestQueue().add(request);
	}
//	public static class TimeCount extends CountDownTimer {
//		public TimeCount(long millisInFuture, long countDownInterval) {
//			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
//		}
//
//		@Override
//		public void onFinish() {// 计时完毕时触发
//			yzmbutton.setText("重新验证");
//			yzmbutton.setClickable(true);
//		}
//
//		@Override
//		public void onTick(long millisUntilFinished) {// 计时过程显示
//			yzmbutton.setClickable(false);
//			yzmbutton.setText(millisUntilFinished / 1000 + "秒");
//		}
//	}
	
	public void login() {
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>().apiVer("100")
				.typeKey("BY_User_login")
				.param("username", phoneedit.getText().toString())
				.param("password", passwordedit.getText().toString())
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						
						SPUtils.put(App.ctx, null, SPContants.WX_Openid, response.getWxopenid());
						SPUtils.put(App.ctx, null, SPContants.USERNAME, response.getUsername());
						SPUtils.put(App.ctx, null, SPContants.MOBILE, response.getMobile());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);
						SPUtils.put(App.ctx, null, SPContants.EMAIL, response.getEmail());
						SPUtils.put(App.ctx, null, SPContants.NICK, response.getNickname());
						SPUtils.put(App.ctx, null, SPContants.PASSWORD, response.getPassword());
						SPUtils.put(App.ctx, null, SPContants.USER_ID, response.getUid());
						SPUtils.put(App.ctx, null, SPContants.AVATAR_URL, response.getHead());
						
						Intent intent1=new Intent(PersonSettingActivity.EXITCHANGE);
					    sendBroadcast(intent1);
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx,"网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
